package com.kl.controller;


import com.kl.domain.WaybillDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Waybill;
import com.kl.feign.CollectionServiceFeign;
import com.kl.feign.OrderServcieFeign;
import com.kl.service.IWaybillService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/waybillfindAll")
public class WaybillController {

    @Autowired
    private IWaybillService iWaybillService;

    @Autowired
    private OrderServcieFeign orderServcieFeign;

    @Autowired
    private CollectionServiceFeign collectionServiceFeign;


    @ApiOperation(value = "根据订单id查询运单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true,
                    paramType = "query", dataTypeClass = Long.class)
    })
    @GetMapping("/findWaybillByOrderId")
    public Result<List<Waybill> > findWaybillByOrderId(@RequestParam Long orderId){
        List<Waybill> waybills = iWaybillService.findWaybillByOrderId(orderId);
        return Result.build(ConstantEnum.SUCCESS).setResult(waybills);
    }


    @ApiOperation(value = "根据手机号查询运单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "consigneePhone", value = "收件人手机号", required = true,
                    paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/findWaybillByPhone/{consigneePhone}")
    public Result<List<WaybillDTO>> findWaybillByPhone(@PathVariable("consigneePhone") String consigneePhone){
        Result<List<WaybillDTO>> waybillByPhone = orderServcieFeign.findWaybillByPhone(consigneePhone);
        return Result.build(ConstantEnum.SUCCESS).setResult(waybillByPhone);
    }


    @ApiOperation(value = "根据运单id查询运单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waybillId", value = "运单id", required = true,
                    paramType = "query", dataTypeClass = Long.class)
    })
    @GetMapping("/findWaybillById")
    public Result<List<WaybillDTO> > findWaybillByWaybillId(@RequestParam Long waybillId){
        Result<WaybillDTO> waybill = orderServcieFeign.findWaybill(waybillId);
        return Result.build(ConstantEnum.SUCCESS).setResult(waybill);
    }


    @ApiOperation(value = "在线理赔")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waybillId", value = "运单id", required = true,
                    paramType = "query", dataTypeClass = Long.class)
    })
    @GetMapping("/claimByWaybillId")
    public Result<List<WaybillDTO> > claimByWaybillId(@RequestParam Long waybillId){
        Result<WaybillDTO> waybill = orderServcieFeign.findWaybill(waybillId);
        Result result = null;
        if(Integer.parseInt(waybill.getResult().getLogisticsStatus()) > 5){
            result = collectionServiceFeign.insertCollection(String.valueOf(waybillId), waybill.getResult().getLogisticsStatus());
        }
        return result;
    }



}
